package com.finger.forest.listener;

import java.util.HashMap;
import java.util.Map;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.finger.forest.base.BaseApi;
import com.finger.forest.utils.RequestUtil;
import com.finger.forest.utils.encode.PrefsUtil;

/**
 * @author jzj
 * @desc GPS位置监听器
 * @date 2021/1/29 0029
 */
public class GpsLocationListener implements LocationListener {

    private static final String TAG = "GpsLocationListener";

    private static String gridUserId = "";
    private static String patrolId = "";

    public GpsLocationListener(String gridUserId, String patrolId) {
        super();
        this.gridUserId = gridUserId;
        this.patrolId = patrolId;
    }

    @Override
    public void onLocationChanged(Location location) {
        // get data and post to the server
        Log.i(TAG, "位置信息："+location);

        if(location != null) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            Log.i(TAG, "获取位置信息成功===>经度："+longitude+"；纬度："+latitude);

            //上传位置到服务器
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Map<String, Object> maps = new HashMap<>();
                    maps.put("gridUserId",Integer.parseInt(gridUserId));
                    maps.put("patrolId",Integer.parseInt(patrolId));
                    maps.put("longitude",longitude);
                    maps.put("latitude",latitude);
                    String result = RequestUtil.httpPost(BaseApi.UPLOAD_LOCATION, maps);
                    if (result != null){
                        Log.i(TAG, result);
                        JSONObject jo = JSON.parseObject(result);

                    }
                }
            }).start();
        }
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}